package com.rerijaapps.preguntaloquequieras;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.rerijaapps.preguntaloquequieras.adaptadores.preguntas_adapter;
import com.rerijaapps.preguntaloquequieras.data.PreguntaData;
import com.rerijaapps.preguntaloquequieras.utils.ListViewPer;

import java.util.ArrayList;

public class PantallaPrincipal extends Activity implements AdapterView.OnItemClickListener {

    private ImageView pp_iv_logo;
    private TextView pp_app_name;
    private ListViewPer pp_lv_preguntas;
    private RelativeLayout pp_relative;
    private boolean list_clicable = true;
    private SlidingDrawer drawer;
    private ImageView hide_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pp_layout);

        //Variable declarations
        pp_iv_logo = (ImageView) findViewById(R.id.pp_iv_logo);
        pp_app_name = (TextView) findViewById(R.id.pp_tv_app_name);
        pp_lv_preguntas = (ListViewPer) findViewById(R.id.pp_lv_preguntas);
        pp_relative = (RelativeLayout) findViewById(R.id.pp_relative);
        drawer = (SlidingDrawer) findViewById(R.id.pp_sliding_drawer);
        hide_layout = (ImageView) findViewById(R.id.pp_hide_layout);

        //Events
        drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                ((ImageButton)drawer.getHandle()).setImageResource(R.drawable.btn_down);
                hide_layout.setVisibility(View.VISIBLE);
                hide_layout.startAnimation(AnimationUtils.loadAnimation(PantallaPrincipal.this, R.anim.fade_in));
            }
        });

        drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                ((ImageButton)drawer.getHandle()).setImageResource(R.drawable.btn_up);
                hide_layout.setVisibility(View.INVISIBLE);
                hide_layout.startAnimation(AnimationUtils.loadAnimation(PantallaPrincipal.this, R.anim.fade_out));
            }
        });


        //Iniciar screen
        loadScreen();

        ArrayList<PreguntaData> data = new ArrayList<PreguntaData>();
        data.add(new PreguntaData()); data.add(new PreguntaData()); data.add(new PreguntaData()); data.add(new PreguntaData());

        pp_lv_preguntas.setAdapter(new preguntas_adapter(this, data));
        pp_lv_preguntas.setOnItemClickListener(this);



    }

    private void loadScreen(){
        pp_relative.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, final View view, int position, long l) {

        if(list_clicable){
            list_clicable = false;
            final RelativeLayout lp_layout = (RelativeLayout) view.findViewById(R.id.lp_relative);

            // Set Animations
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_left);

            anim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    lp_layout.setAlpha(0);
                    pp_relative.setVisibility(View.GONE);
                    pp_relative.startAnimation(AnimationUtils.loadAnimation(PantallaPrincipal.this, R.anim.fade_out));
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            lp_layout.startAnimation(anim);
            pp_lv_preguntas.setVisibility(View.GONE);
            pp_lv_preguntas.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
        }



    }
}
